package com.ymx.darling.session.setting;

import com.ymx.darling.dialect.page.PageBody;
import com.ymx.darling.log.Log;
import com.ymx.darling.session.setting.relation.RelationSelector;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 全局静态上下文
 * 特别注意:该静态类为框架内部全局资源处理类,请勿随意修改
 * @author 爱Java的小于
 */
public class Context {
    private static final ThreadLocal<StatementSetting> statementSettingThreadLocal = new ThreadLocal<>();

    public static void clearSetting() {
        statementSettingThreadLocal.remove();
    }

    private static StatementSetting getStatementSetting() {
        StatementSetting statementSetting = statementSettingThreadLocal.get();
        if (statementSetting == null) {
            statementSetting = new StatementSetting();
            statementSettingThreadLocal.set(statementSetting);
        }

        return statementSetting;
    }

    public static boolean hasInsertBatchSize() {
        return getStatementSetting().getBatchSize() != null;
    }

    public static boolean hasSqlStatementId() {
        return Objects.nonNull(getStatementSetting().getSqlStatementId());
    }

    public static boolean hasNestedParam() {
        return Objects.nonNull(getStatementSetting().getNestedQueryParam());
    }

    public static boolean hasPageBody() {
        return Objects.nonNull(getStatementSetting().getPageBody());
    }

    public static boolean hasNamespace() {
        return Objects.nonNull(getStatementSetting().getNamespaceClass());
    }

    public static boolean hasMapperMethod() {
        return Objects.nonNull(getStatementSetting().getMapperMethod());
    }

    public static boolean hasStatementLog() {
        return Objects.nonNull(getStatementSetting().getStatementLog());
    }

    public static boolean hasRelationSelector() {
        return Objects.nonNull(getStatementSetting().getRelationSelector());
    }

    public static void setInsertBatchSize(int batchCount) {
        if (batchCount < 1) {
            throw new IllegalArgumentException("批量插入时不能将批量大小设置为:"+batchCount);
        }
        getStatementSetting().setBatchSize(batchCount);
    }

    public static int getInsertBatchSize() {
        return getStatementSetting().getBatchSize();
    }

    public static void setSqlStatementId(String sqlStatementId) {
        getStatementSetting().setSqlStatementId(sqlStatementId);
    }

    public static void setRelationSelector(RelationSelector relationSelector) {
        getStatementSetting().setRelationSelector(relationSelector);
    }


    public static RelationSelector getRelationSelector() {
        return getStatementSetting().getRelationSelector();
    }

    public static void setPage(PageBody pageBody) {
        getStatementSetting().setPageBody(pageBody);
    }

    public static PageBody getPageBody() {
        return getStatementSetting().getPageBody();
    }

    public static void setNamespace(Class<?> mapperClass) {
        getStatementSetting().setNamespaceClass(mapperClass);
    }

    public static String getSqlStatementId() {
        return getStatementSetting().getSqlStatementId();
    }

    public static Class<?> getNamespace() {
        return getStatementSetting().getNamespaceClass();
    }

    public static void setNestedParam(String key, Object value) {
        Map<String, Object> nestedParamMap = getStatementSetting().getNestedQueryParam();
        if (Objects.isNull(nestedParamMap)) {
            nestedParamMap = new HashMap<>();
            getStatementSetting().setNestedQueryParam(nestedParamMap);
        }
        nestedParamMap.put(key, value);
    }

    public static Map<String, Object> getNestedParam() {
        return getStatementSetting().getNestedQueryParam();
    }

    public static void setMapperMethod(Method method) {
        getStatementSetting().setMapperMethod(method);
    }

    public static Method getMapperMethod() {
        return getStatementSetting().getMapperMethod();
    }

    public static void setStatementLog(Log statementLog) {
        getStatementSetting().setStatementLog(statementLog);
    }

    public static Log getStatementLog() {
        return getStatementSetting().getStatementLog();
    }
}
